﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_FIN_RETURN_ACCOUNT_ORDER
// * CreateDate    : 2013/1/22 10:03:38
// * Creater       : FinReturnAccountOrderBs
// * LastChangeDate: 2013/1/22 10:03:38
// * LastChanger   : 2013/1/22 10:03:38
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;

namespace ESOL.Business.Fin
{
    /// <summary>
    /// FinReturnAccountOrder
    /// </summary>
    public partial class FinReturnAccountOrderBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objFinReturnAccountOrderEntity">Entity</param>
        /// <returns>Entity</returns>
        public static FinReturnAccountOrderEntity GetFinReturnAccountOrder(FinReturnAccountOrderEntity objFinReturnAccountOrderEntity)
        {
            return DataAccess.SelectSingle<FinReturnAccountOrderEntity>(objFinReturnAccountOrderEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objFinReturnAccountOrderEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertFinReturnAccountOrder(FinReturnAccountOrderEntity objFinReturnAccountOrderEntity)
        {
            return DataAccess.Insert(objFinReturnAccountOrderEntity);
        }

        public static bool InsertFinReturnAccountOrder(List<FinReturnAccountOrderEntity> objFinReturnAccountOrderEntityList)
        {
            return DataAccess.Insert(objFinReturnAccountOrderEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objFinReturnAccountOrderEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateFinReturnAccountOrder(FinReturnAccountOrderEntity objFinReturnAccountOrderEntity)
        {
            return DataAccess.Update(objFinReturnAccountOrderEntity);
        }

        public static bool UpdateFinReturnAccountOrder(List<FinReturnAccountOrderEntity> objFinReturnAccountOrderEntityList)
        {
            return DataAccess.Update<FinReturnAccountOrderEntity>(objFinReturnAccountOrderEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objFinReturnAccountOrderEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteFinReturnAccountOrder(FinReturnAccountOrderEntity objFinReturnAccountOrderEntity)
        {
            return DataAccess.Delete(objFinReturnAccountOrderEntity);
        }

        public static bool DeleteFinReturnAccountOrder(List<FinReturnAccountOrderEntity> objFinReturnAccountOrderEntityList)
        {
            return DataAccess.Delete<FinReturnAccountOrderEntity>(objFinReturnAccountOrderEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetFinReturnAccountOrderAll()
        {
            return DataAccess.SelectAllDataSet<FinReturnAccountOrderEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<FinReturnAccountOrderEntity> GetFinReturnAccountOrderAllList()
        {
            return DataAccess.SelectAll<FinReturnAccountOrderEntity>();
        }
        #endregion

        public static bool SaveReturnAccountOrder(FinReturnAccountOrderEntity entity,bool isNew)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (isNew)
                    {
                        string code = "";
                        if (MstCodeRuleBs.GetCurrentOrderCode("FinReturnAccountOrder", out code, broker) == false)
                        {
                            broker.RollBack();
                            throw new Exception("自动生成发货申请单号失败！");
                        }
                        entity.OrderCode = code;
                        DataAccess.Insert(entity, broker);
                    }
                    else
                    {
                        DataAccess.Update(entity, broker);
                    }

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }

        }

        public static bool ApproveReturnAccountOrder(FinReturnAccountOrderEntity entity, string msg)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Update(entity, broker);

                    //应收账款
                    FinCustomerAccountItemEntity accountItem = new FinCustomerAccountItemEntity();
                    accountItem.CustomerAccountItemId = Guid.NewGuid().ToString();
                    accountItem.CustomerId = entity.CustomerId;
                    accountItem.AmountDate = entity.OrderDate;
                    accountItem.AmountPerson = entity.CreatedBy;
                    accountItem.RelatedOrderId = entity.Id;
                    accountItem.RelatedOrderCode = entity.OrderCode;
                    SalesReturnOrderEntity returnOrder = SalesReturnOrderBs.GetSalesReturnOrderById(entity.SalesReturnOrderId);
                    accountItem.ApplicationId = returnOrder.ApplicationId;
                    accountItem.ReceivableAmount = -Convert.ToDecimal(entity.TotalAmount);

                    DataAccess.Insert(accountItem, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static DataTable GetByCondtion(string condition, string orderBy, string RowCount)
        {
            string sql = CommonFunctions.GetSelect(RowCount) + @" A.*,B.ORDER_CODE AS RETURN_CODE,C.CUSTOMER_NAME,D.APPLICATION_CODE,E.NAME_CN FROM dbo.FIN_RETURN_ACCOUNT_ORDER A 
                                                                 LEFT JOIN dbo.SALES_RETURN_ORDER B ON A.SALES_RETURN_ORDER_ID=B.ID
                                                                 LEFT JOIN dbo.MST_CUSTOMER C ON A.CUSTOMER_ID=C.CUSTOMER_ID
                                                                 LEFT JOIN dbo.SALES_PROD_PLAN_APPLICATION D ON B.APPLICATION_ID=D.APPLICATION_ID
                                                                 LEFT JOIN dbo.MST_PERSON E ON A.CREATED_BY=E.PERSON_ID
                                                                 WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
           
    }


}

